<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Editor{
	var $CI;
	var $script;
	 function __construct() {
		$this->CI =& get_instance();
    	$this->CI->load->helper('url');
    	$this->CI->headassets->addScript(true,'js/editors/tinymce/jscripts/tiny_mce.js');
		
	 }
	function init($selector,$media = true){
		$output = '';
		$call = '';
		$calljs = '';
		if($media == true){
			$call .= 'file_browser_callback: "openKCFinder",';
			$calljs .='function openKCFinder(field_name, url, type, win) {
					tinyMCE.activeEditor.windowManager.open({
						file: "js/editors/kcfinder/browse.php?opener=tinymce&type=" + type,
						title: "Images",
						width: 700,
						height: 500,
						resizable: "yes",
						inline: true,
						close_previous: "yes",
						popup_css: false
					}, {
						window: win,
						input: field_name
					});
					return false;
				}';
		}
			$output = '
				tinyMCE.init({
					// General options
					'.$call.'
					mode : "exact",
					theme : "advanced",
					elements : "'.$selector.'",
					relative_urls : true,
					remove_script_host : false,
					document_base_url : "'.base_url().'", 
					plugins : "autolink,lists,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,autosave,visualblocks",
					
					// Cleanup/Output
					inline_styles : true,
					gecko_spellcheck : true,
					cleanup : true,
					cleanup_on_startup : false,
					entity_encoding : "raw",
					extended_valid_elements : "hr[id|title|alt|class|width|size|noshade|style],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name|style],a[id|class|name|href|target|title|onclick|rel|style]",
					force_br_newlines : false, force_p_newlines : true, forced_root_block : \'p\',
					invalid_elements : "applet",
					
					// Theme options
					theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
					theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
					theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
					theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak,restoredraft,visualblocks",
					theme_advanced_toolbar_location : "top",
					theme_advanced_toolbar_align : "left",
					theme_advanced_statusbar_location : "bottom",
					theme_advanced_resizing : false,
					plugin_insertdate_dateFormat : "%d-%m-%Y",
					plugin_insertdate_timeFormat : "%H:%M:%S", 
					
					// Layout
					content_css : "'.base_url().'templates/backend/css/editor.css", 
					// Drop lists for link/image/media/template dialogs
					//template_external_list_url : "lists/template_list.js",
					//external_link_list_url : "lists/link_list.js",
					//external_image_list_url : "lists/image_list.js",
					//media_external_list_url : "lists/media_list.js",
				});
				'.$calljs.'
				';
		return $this->CI->headassets->addScript(false,jsScript($output));
	}
	function display($name, $content, $width, $height)
	{
		if (is_numeric( $width )) {
			$width .= 'px';
		}
		if (is_numeric( $height )) {
			$height .= 'px';
		}
		$editor  = '<textarea id="'.$name.'" name="'.$name.'" cols="'.$col.'" rows="'.$row.'" style="width:'.$width.'; height:'.$height.';" class="mce_editable">'.$content.'</textarea>\n' ;
		return $editor;
	}
}